package cn.itcast.bos.web.action.standard;



import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.standard.StandardService;
import cn.itcast.bos.web.action.base.BaseAction;

import com.opensymphony.xwork2.ActionContext;
@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class StandardAction extends BaseAction<Standard>{
	
	@Autowired
	private StandardService standardService;

	// 数据作废的方法
	@Action(value="standard_delete",results={@Result(name="success",type="json")})
	public String delete(){
		String ids = ServletActionContext.getRequest().getParameter("ids");
		standardService.delete(ids);		
		ActionContext.getContext().getValueStack().push("1");
		
		return "success";
	}
	
	
	//还原的方法
	@Action(value="standard_restore",results={@Result(name="success",type="json")})
	public String restore(){
		String ids = ServletActionContext.getRequest().getParameter("ids");
		standardService.restore(ids);
		
		ActionContext.getContext().getValueStack().push("1");
		
		return "success";
	}
	
	
	// combobox 下拉列表的数据显示请求
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard>list=standardService.findAll();
		//把数据放到值栈中   后台才能将数据返回到页面
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	
	
	//显示分页
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuerey(){
		//封装数据 page从0开始 
		Pageable pageable= new PageRequest(page-1, rows);
		//条件查询多了name="aa"
		Specification<Standard> specification= new Specification<Standard>() {

			
			public Predicate toPredicate(Root<Standard> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate  p1=null;
				//如果页面传来的数据步为空
				if(StringUtils.isNotBlank(model.getName())){
					//模糊查询
					p1=	cb.like(root.get("name").as(String.class), "%"+model.getName()+"%");
				}			
				return p1;
			}
		};
		
		Page<Standard>page=standardService.findPageData(specification,pageable);
		
		pushPageDataToValueStack(page);
	
		return SUCCESS;
	}
	
	

	
	//增加
	@Action(value="standard_save",results={@Result(name="success",
			location="/pages/base/standard.html",type="redirect")})
	public String save(){
		standardService.save(model);
		
		return SUCCESS;
	}

}
